#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
	qDebug() << "Info : start Mainwindow...";
	setupUi(this);//Init de la framme
	GB_Work->setVisible(false);
	Otis = new xmlScribe;
	Edit_FolderOut->setText(QCoreApplication::applicationDirPath() + "/Upload/UpdatesList.xml");
	CBox_OldUpdatesLst->setEnabled(true);
	if(CBoxChkDoublon->checkState() == 0)
	{treeWidget->setCheckDoublon(false);}
	else if (CBoxChkDoublon->checkState() == 2)
	{treeWidget->setCheckDoublon(true);}
	if(CBoxAfficherMsgBox->checkState() == 0)
	{treeWidget->setfloodMsgBox(false);}
	else if (CBoxAfficherMsgBox->checkState() == 2)
	{treeWidget->setfloodMsgBox(true);}
	GB_edit->setVisible(false);
	Lbl_OldUpdatesLst->setEnabled(false);
	Edit_OldUpdatesLst->setEnabled(false);
	Btn_OkOldUpdatesLst->setEnabled(false);
	Btn_SearchOldUpdatesLst->setEnabled(false);
	connectland();
	Set_Status_Msg("Welcome to the APDVM builder");
	treeWidget->setFilelevel(SpinBox_level->value());
	adjustSize();
}
MainWindow::~MainWindow()
{
	delete Otis;
	qDebug() << "Info :  stop Mainwindow...";
}
void MainWindow::connectland()
{
	connect(Btn_Close, SIGNAL(clicked()), qApp, SLOT(closeAllWindows()));
	connect(Btn_First_Ignore, SIGNAL(clicked()),this, SLOT(START_IgnorePath()));
	connect(Btn_First_Ok, SIGNAL(clicked()),this, SLOT(START_SetUpdaterPath()));
	connect(Btn_First_Search, SIGNAL(clicked()),this, SLOT(START_SearchPathUpdater()));
	connect(CBox_OldUpdatesLst, SIGNAL(stateChanged(int)), this, SLOT(Enable_Old_List(int)));
	connect(Btn_Item_addfolder, SIGNAL(clicked()),treeWidget, SLOT(AddFolder()));
	connect(Btn_Item_addfile, SIGNAL(clicked()),treeWidget, SLOT(AddFile()));
	connect(Btn_Item_remove, SIGNAL(clicked()),treeWidget, SLOT(DeleteSelectedFile()));
	connect(Btn_Apply_Edit, SIGNAL(clicked()), this, SLOT(Liste_Edit_Apply()));
	connect(SpinBox_level, SIGNAL(valueChanged(int)),treeWidget, SLOT(setFilelevel(int)));
	connect(CBoxChkDoublon, SIGNAL(stateChanged(int)), this, SLOT(EnableCheckDoublon(int)));
	connect(CBoxAfficherMsgBox, SIGNAL(stateChanged(int)), this, SLOT(EnableMsgBox(int)));
	connect(treeWidget,SIGNAL(TaskToShowInStatusbar(QString)), this, SLOT(Set_Status_Msg(QString)));
	connect(treeWidget,SIGNAL(TaskToShowGbEdit(bool)), this, SLOT(Liste_Show_Edit(bool)));
	connect(treeWidget,SIGNAL(TaskToEnableGUI(bool)), this, SLOT(EnableControls(bool)));
	connect(Btn_SearchOldUpdatesLst, SIGNAL(clicked()), this, SLOT(BrowseXML()));
	connect(Btn_OkOldUpdatesLst, SIGNAL(clicked()), this, SLOT(ReadXML()));
	connect(Otis, SIGNAL(TasktoAddFileIntoTree(QString, QString, QString, int)), treeWidget, SLOT(InsertFileFromXML(QString, QString, QString, int)));
	connect(treeWidget, SIGNAL(OneItemWasAdded()), this, SLOT(DisableCBox_Old_List()));
	connect(Btn_Apply, SIGNAL(clicked()), this, SLOT(LaunchGen()));
	connect(&Generator,SIGNAL(CanYouShowErrorMsg(QString)),this,SLOT(ShowMsgError(QString)));
	connect(&Generator,SIGNAL(CanYouShowMsg(QString)),this,SLOT(ShowMsg(QString)));
	connect(&Generator,SIGNAL(CanYouShowProgress(int, int)),this,SLOT(Showprogress(int, int)));
}
void MainWindow::ShowMsgError(QString msg)
{
	this->Set_Status_Msg(msg);
	//TODO : msgBox
}
void MainWindow::ShowMsg(QString msg)
{
	this->Set_Status_Msg(msg);
	//TODO : msgBox
}
void MainWindow::Showprogress(int value, int value_max)
{
	Prgbar_Main->setValue(value);
	Prgbar_Main->setMaximum(value_max);
}
void MainWindow::START_SearchPathUpdater()
{
	bool err = false;
	int countFile = 0;
	QString fileName = QFileDialog::getOpenFileName(
                         this,
                         trUtf8("Selectionnez l'updater"));

	Line_First_Select->setText(fileName);
	if (!fileName.isEmpty())
	{
		Btn_First_Ok->setEnabled(true);
	}
}
void MainWindow::START_SetUpdaterPath()
{
	FirstWidget->setVisible(false);
	GB_Work->setVisible(true);
	adjustSize();
	QFileInfo FI_updater(Line_First_Select->text());
	PATH_Updater = FI_updater.path();
	treeWidget->Set_Path_Updater(PATH_Updater);
	Set_Status_Msg("Updater choisi dans le dossier : " + PATH_Updater);
	treeWidget->Insert_updater(PATH_Updater, FI_updater.fileName(), 100);
}
void MainWindow::START_IgnorePath()
{
	FirstWidget->setVisible(false);
	GB_Work->setVisible(true);
	adjustSize();
}
void MainWindow::BrowseXML()
{
	QString fileName = QFileDialog::getOpenFileName(
                         this,
                         trUtf8("Selectionnez le fichier a lire"));
	if (fileName.isEmpty())
	{return;}
	else
	{Edit_OldUpdatesLst->setText(fileName);}
}

void MainWindow::ReadXML()
{
	QFileInfo XML_list(Edit_OldUpdatesLst->text());
	Edit_FolderOut->setText(XML_list.absoluteFilePath());
	PATH_XMLList = XML_list.path();
	Generator.setPathXML(PATH_XMLList);
	Otis->ReadFile(Edit_OldUpdatesLst->text());
	Btn_Apply->setEnabled(true);
}

void MainWindow::Enable_Old_List(int newstatus)
{
	if(newstatus == 0)
	{
		Lbl_OldUpdatesLst->setEnabled(false);
		Edit_OldUpdatesLst->setEnabled(false);
		Btn_OkOldUpdatesLst->setEnabled(false);
		Btn_SearchOldUpdatesLst->setEnabled(false);
		Btn_SearchFolderOut->setEnabled(true);
		Edit_FolderOut->setEnabled(true);
		Lbl_FolderOut->setEnabled(true);
	}
	else if (newstatus == 2)
	{
		Lbl_OldUpdatesLst->setEnabled(true);
		Edit_OldUpdatesLst->setEnabled(true);
		Btn_OkOldUpdatesLst->setEnabled(true);
		Btn_SearchOldUpdatesLst->setEnabled(true);
		Btn_SearchFolderOut->setEnabled(false);
		Edit_FolderOut->setEnabled(false);
		Lbl_FolderOut->setEnabled(false);
	}
}
void MainWindow::DisableCBox_Old_List()
{
	CBox_OldUpdatesLst->setEnabled(false);
	Lbl_OldUpdatesLst->setEnabled(false);
	Edit_OldUpdatesLst->setEnabled(false);
	Btn_OkOldUpdatesLst->setEnabled(false);
	Btn_SearchOldUpdatesLst->setEnabled(false);
	disconnect(treeWidget, SIGNAL(OneItemWasAdded()), this, SLOT(DisableCBox_Old_List()));
}
void MainWindow::EnableCheckDoublon(int newstatus)
{
	if(newstatus == 0)
	{treeWidget->setCheckDoublon(false);}
	else if (newstatus == 2)
	{treeWidget->setCheckDoublon(true);}
}

void MainWindow::EnableMsgBox(int newstatus)
{
	if(newstatus == 0)
	{treeWidget->setfloodMsgBox(false);}
	else if (newstatus == 2)
	{treeWidget->setfloodMsgBox(true);}
}

void MainWindow::Set_Status_Msg(QString new_text, int time_showed)
{
	statusBar()->showMessage(new_text, time_showed);
	
}
void MainWindow::Liste_Show_Edit(bool newState)
{
	GB_edit->setVisible(newState);
	SpinBox_levelEdit->setValue(50);
}
void MainWindow::Liste_Edit_Apply()
{
	treeWidget->SetLevelSelectedFiles(SpinBox_levelEdit->value());
}
void MainWindow::EnableControls(bool setval)
{
	Btn_Item_addfolder->setEnabled(setval);
	Btn_Item_addfile->setEnabled(setval);
	SpinBox_level->setEnabled(setval);
	Btn_Item_remove->setEnabled(setval);
	GB_edit->setEnabled(setval);
	tabWidget->setEnabled(setval);
	Btn_Apply->setEnabled(setval);
}
void MainWindow::LaunchGen()
{
	Generator.setLists(treeWidget->getList());
	Otis->Set_Path_Updater(PATH_Updater);
	if(Otis->WriteFile(Generator.getXMLList(),Edit_FolderOut->text()) == 0)
	{
		Generator.setPathXML(Edit_FolderOut->text());
		Generator.setPathUpdater(PATH_Updater);
		Generator.Build();
	}
	else
	{
		//TODO : Erreur de création du fichier XML
	}
}